<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-button v-hasPermi="['admin','ums:memberlevel:add']" type="primary" small class="addBtn" style="margin:10px" size="mini" icon="el-icon-plus" @click="addDialogVisible = true">新增
      </el-button>
      <!--表格-->
      <el-table :data="listForm" stripe border style="width: 100%">
        <el-table-column prop="id" header-align="center" align="center" label="id" />
        <el-table-column prop="name" header-align="center" align="center" label="等级名称" />
        <el-table-column prop="growthPoint" header-align="center" align="center" label="成长值" />
        <el-table-column prop="defaultStatus" header-align="center" align="center" label="默认等级">
          <template slot-scope="scope">
            <i v-if="scope.row.defaultStatus==1" class="el-icon-success" />
            <i v-else class="el-icon-error" />
          </template>
        </el-table-column>
        <el-table-column prop="freeFreightPoint" header-align="center" align="center" label="免运费标准" />
        <el-table-column prop="commentGrowthPoint" header-align="center" align="center" label="每次评价获取的成长值" />
        <el-table-column prop="priviledgeFreeFreight" header-align="center" align="center" label="免邮特权">
          <template slot-scope="scope">
            <i v-if="scope.row.priviledgeFreeFreight==1" class="el-icon-success" />
            <i v-else class="el-icon-error" />
          </template>
        </el-table-column>
        <el-table-column prop="priviledgeMemberPrice" header-align="center" align="center" label="会员价格特权">
          <template slot-scope="scope">
            <i v-if="scope.row.priviledgeMemberPrice==1" class="el-icon-success" />
            <i v-else class="el-icon-error" />
          </template>
        </el-table-column>
        <el-table-column prop="priviledgeBirthday" header-align="center" align="center" label="生日特权">
          <template slot-scope="scope">
            <i v-if="scope.row.priviledgeBirthday==1" class="el-icon-success" />
            <i v-else class="el-icon-error" />
          </template>
        </el-table-column>
        <el-table-column prop="note" header-align="center" align="center" label="备注" />
        <!--操作-->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip :enterable="false" effect="dark" content="编辑" placement="top">
              <el-button v-hasPermi="['admin','ums:memberlevel:edit']" type="text" size="mini" @click="editBtn(scope.row.id)">编辑</el-button>
            </el-tooltip>
            <el-tooltip :enterable="false" effect="dark" content="删除" placement="top">
              <el-button v-hasPermi="['admin','ums:memberlevel:del']" type="text" size="mini" @click="delBtn(scope.row.id)">删除</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        background
        :current-page="pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

    </el-card>

    <!--添加对话框-->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="580px" @close="addHandleClose">
      <span>
        <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="100px">
          <el-form-item label="等级名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="等级成长值" prop="growthPoint">
            <el-input-number
              v-model.number="addForm.growthPoint"
              :min="1"
              :max="99999"
              controls-position="right"
              style="width: 178px;"
            />
          </el-form-item>
          <el-form-item label="默认等级" prop="defaultStatus">
            <el-checkbox v-model="addForm.defaultStatus" :true-label="1" :false-label="0" />
          </el-form-item>
          <el-form-item label="免运费标准" prop="freeFreightPoint">
            <el-input-number
              v-model.number="addForm.freeFreightPoint"
              :min="1"
              :max="99999"
              controls-position="right"
              style="width: 178px;"
            />
          </el-form-item>
          <el-form-item label="评价成长值" prop="commentGrowthPoint">
            <el-input-number
              v-model.number="addForm.commentGrowthPoint"
              :min="1"
              :max="99999"
              controls-position="right"
              style="width: 178px;"
            />
          </el-form-item>
          <el-form-item label="免邮特权" prop="priviledgeFreeFreight">
            <el-checkbox v-model="addForm.priviledgeFreeFreight" :true-label="1" :false-label="0" />
          </el-form-item>
          <el-form-item label="会员价格特权" prop="priviledgeMemberPrice">
            <el-checkbox v-model="addForm.priviledgeMemberPrice" :true-label="1" :false-label="0" />
          </el-form-item>
          <el-form-item label="生日特权" prop="priviledgeBirthday">
            <el-checkbox v-model="addForm.priviledgeBirthday" :true-label="1" :false-label="0" />
          </el-form-item>
          <el-form-item label="备注" prop="note">
            <el-input v-model="addForm.note" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="580px" @close="editHandleClose">
      <span>
        <el-form ref="editFormRef" :model="editForm" label-width="100px">
          <el-form-item label="等级名称" prop="name">
            <el-input v-model="editForm.name" />
          </el-form-item>
          <el-form-item label="等级成长值" prop="growthPoint">
            <el-input-number
              v-model.number="editForm.growthPoint"
              :min="1"
              :max="99999"
              controls-position="right"
              style="width: 178px;"
            />
          </el-form-item>
          <el-form-item label="默认等级" prop="defaultStatus">
            <el-checkbox v-model="editForm.defaultStatus" :true-label="1" :false-label="0" />
          </el-form-item>
          <el-form-item label="免运费标准" prop="freeFreightPoint">
            <el-input-number
              v-model.number="editForm.freeFreightPoint"
              :min="1"
              :max="99999"
              controls-position="right"
              style="width: 178px;"
            />
          </el-form-item>
          <el-form-item label="评价成长值" prop="commentGrowthPoint">
            <el-input-number
              v-model.number="editForm.commentGrowthPoint"
              :min="1"
              :max="99999"
              controls-position="right"
              style="width: 178px;"
            />
          </el-form-item>
          <el-form-item label="免邮特权" prop="priviledgeFreeFreight">
            <el-checkbox v-model="editForm.priviledgeFreeFreight" :true-label="1" :false-label="0" />
          </el-form-item>
          <el-form-item label="会员价格特权" prop="priviledgeMemberPrice">
            <el-checkbox v-model="editForm.priviledgeMemberPrice" :true-label="1" :false-label="0" />
          </el-form-item>
          <el-form-item label="生日特权" prop="priviledgeBirthday">
            <el-checkbox v-model="editForm.priviledgeBirthday" :true-label="1" :false-label="0" />
          </el-form-item>
          <el-form-item label="备注" prop="note">
            <el-input v-model="editForm.note" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit, del, getMemberLevel, listMemberLevel } from '@/api/member/memberlevel'

export default {
  name: 'Level',
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      listForm: [],
      addForm: {
        name: '',
        growthPoint: 1,
        defaultStatus: 0,
        freeFreightPoint: 1,
        commentGrowthPoint: 1,
        priviledgeFreeFreight: 0,
        priviledgeMemberPrice: 0,
        priviledgeBirthday: 0,
        note: ''
      },
      editForm: {},
      addFormRules: {

        name: [
          { required: true, message: '请输入等级名称', trigger: 'blur' }
        ],
        growthPoint: [
          { required: true, message: '请输入等级需要的成长值', trigger: 'blur' }
        ],
        defaultStatus: [
          { required: true, message: '请输入是否为默认等级[0->不是；1->是]', trigger: 'blur' }
        ],
        freeFreightPoint: [
          { required: true, message: '请输入免运费标准', trigger: 'blur' }
        ],
        commentGrowthPoint: [
          { required: true, message: '请输入每次评价获取的成长值', trigger: 'blur' }
        ],
        priviledgeFreeFreight: [
          { required: true, message: '请输入是否有免邮特权', trigger: 'blur' }
        ],
        priviledgeMemberPrice: [
          { required: true, message: '请输入是否有会员价格特权', trigger: 'blur' }
        ],
        priviledgeBirthday: [
          { required: true, message: '请输入是否有生日特权', trigger: 'blur' }
        ],
        note: [
          { required: true, message: '请输入备注', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 初始化列表
    this.initListMemberLevel()
  },

  methods: {
    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },

    // 确认添加
    addClick() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        add(this.addForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListMemberLevel()
          }
          // 关闭对话框
          this.addDialogVisible = false
        })
      })
    },

    // 编辑按钮
    editBtn(id) {
      this.editDialogVisible = true
      getMemberLevel(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
        }
      })
    },

    // 确认编辑
    editClick() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListMemberLevel()
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 删除按钮
    delBtn(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(id).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListMemberLevel()
          }
        })
      })
    },

    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.initListMemberLevel()
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.initListMemberLevel()
    },

    // 监听添加对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
    },

    // 监听编辑对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
    },

    // 初始化数据
    initListMemberLevel() {
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      listMemberLevel(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    }
  }
}
</script>

<style scoped>
</style>
